﻿using nplJobsCommon.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Insert
{
    public class InsertJob : DataAccessBase
    {
        public int lastJobID = 0;
        public string checkUnique;

        public Job Job { get; set; }

        public InsertJob()
        {
            StoredProcedureName = StoredProcedure.Name.spInsertJobEntry.ToString();
        }

        public void Insert()
        {
            InsertJobDataParameters insertEmployeerDataParameters = new InsertJobDataParameters(this.Job,"I");
            DataBaseHelper dbHelper = new DataBaseHelper(StoredProcedureName);
            dbHelper.Parameters = insertEmployeerDataParameters.Parameters;
            object lastJobID = dbHelper.RunScalar();


            if (lastJobID != null)
            {
                this.lastJobID = int.Parse(lastJobID.ToString());

            }
            //if (insertEmployeerDataParameters.Parameters[24].Value != null)
            //{
            //    lastJobID = insertEmployeerDataParameters.Parameters[24].Value.ToString();
            //}
        }

        public void Update()
        {
            InsertJobDataParameters insertEmployeerDataParameters = new InsertJobDataParameters(this.Job, "U");
            DataBaseHelper dbHelper = new DataBaseHelper(StoredProcedureName) ;//"spUpdateJobEntry");
            dbHelper.Parameters = insertEmployeerDataParameters.Parameters;
            object lastJobID = dbHelper.RunScalar();


            if (lastJobID != null)
            {
                this.lastJobID = int.Parse(lastJobID.ToString());

            }
            //if (insertEmployeerDataParameters.Parameters[24].Value != null)
            //{
            //    lastJobID = insertEmployeerDataParameters.Parameters[24].Value.ToString();
            //}
        }
    }

    #region SQL parameters for job entry
    public class InsertJobDataParameters
    {
        public int lastJobID;
        private SqlParameter[] _parameters;

        public InsertJobDataParameters(Job job,string strMode)
        {
            this.Job = job;
            Build(strMode);
        }

        public InsertJobDataParameters(Job job)
        {
            this.Job = job;
            BuildUpdate();
        }
       

        #region spAddContact parameters
        /*
         
         */
        #endregion

        private void Build(string strMode)
        {
            SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobID", Job.JobID),
                    new SqlParameter("@JobTitle", Job.JobTitle),
                    new SqlParameter("@JobCategoryID", Job.JobCategoryID),
                    new SqlParameter("@JobResponsibilities", Job.JobResponsibilities),
                    new SqlParameter("@JobTypeID", Job.JobTypeID),
                    new SqlParameter("@CityID", Job.CityID ),
                    new SqlParameter("@CareerLevelID", Job.CareerLevelID),
                    new SqlParameter("@PostedDate", Job.PostedDate),
                    new SqlParameter("@ApplicationDateline", Job.ApplicationDateline),
                    new SqlParameter("@NumberOfViews", Job.NumberOfViews),
                    new SqlParameter("@InternalJobCode", Job.InternalJobCode),
                    new SqlParameter("@Benefits", Job.Benefits),
                    new SqlParameter("@Qualifications", Job.Qualifications),
                    new SqlParameter("@Skills", Job.Skills),
                    new SqlParameter("@EmployeerID", Job.EmployeerID),                    
                    new SqlParameter("@SalaryID", Job.SalaryID),
                    new SqlParameter("@ApplicationInstructions", Job.ApplicationInstructions),
                    new SqlParameter("@Vacancies", Job.Vacancies),
                    new SqlParameter("@AlternativeCompanyName", Job.AlternativeCompanyName),
                    new SqlParameter("@IsDeleted", Job.IsDeleted),
                    new SqlParameter("@IsCompanyInfoVisible", Job.IsCompanyInfoVisible),
                    new SqlParameter("@IsContactPersonsVisible",  Job.IsContactPersonsVisible),
                    new SqlParameter("@ContactPersonID", Job.ContactPersonID),
                    new SqlParameter("@IsPhotoEnclosed",  Job.IsPhotoEnclosed),
                    new SqlParameter("@Banner",  null),
                    new SqlParameter("@isOnlineResume",  Job.isOnlineResume),
                    new SqlParameter("@isEmailAttachment",  Job.isEmailAttachment),
                    new SqlParameter("@isHardCopy",  Job.isHardCopy),
                    new SqlParameter("@isGovtJob",  Job.isGovtJob),

                    new SqlParameter("@AgeRangeFrom",  Job.AgeRangeFrom),
                    new SqlParameter("@AgeRangeTo",  Job.AgeRangeTo),
                    new SqlParameter("@Gender",  Job.Gender),
                    new SqlParameter("@RequiredExperience",  Job.RequiredExperience),
                    new SqlParameter("@SalaryRange",  Job.SalaryRange),
                    new SqlParameter("@SalaryRangeMin",  Job.SalaryRangeMin),
                    new SqlParameter("@SalaryRangeMax",  Job.SalaryRangeMax),
                    new SqlParameter("@JobPublishStatus",  Job.JobPublishStatus),
                    new SqlParameter("@JobImagePath",  Job.JobImagePath),

                    new SqlParameter("@TotalYearsOfExpMin",  Job.TotalYearsOfExpMin),
                    new SqlParameter("@TotalYearsOfExpMax",  Job.TotalYearsOfExpMax),
                    new SqlParameter("@ExperienceArea",  Job.ExperienceArea),
                    new SqlParameter("@KnownLanguage",  Job.KnownLanguage),
                    new SqlParameter("@JobLocationType",  Job.JobLocationType),
                    new SqlParameter("@JobSource",  Job.JobSource),
                    //new SqlParameter("@LastJobID",  lastJobID){ Direction= System.Data.ParameterDirection.ReturnValue},
                    new SqlParameter("@strMode",  strMode),
                };
            Parameters = parameters;
        }

        private void BuildUpdate()
        {
            SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobID", Job.JobID),
                   
                    new SqlParameter("@JobResponsibilities", Job.JobResponsibilities),
                   
                    new SqlParameter("@LocationID", Job.CityID ),
                    new SqlParameter("@CareerLevelID", Job.CareerLevelID),
                   
                    new SqlParameter("@Benefits", Job.Benefits),
                    new SqlParameter("@Qualifications", Job.Qualifications),
                    new SqlParameter("@Skills", Job.Skills),
                    new SqlParameter("@SalaryID", Job.SalaryID),
                    
                    new SqlParameter("@ApplicationInstructions", Job.ApplicationInstructions),
                    new SqlParameter("@Vacancies", Job.Vacancies),
                    new SqlParameter("@IsCompanyInfoVisible", Job.IsCompanyInfoVisible),
                    new SqlParameter("@IsOnlineResume", Job.isOnlineResume),
                    new SqlParameter("@IsEmailAttachment", Job.isEmailAttachment),
                    new SqlParameter("@isHardCopy", Job.isHardCopy),
                    new SqlParameter("@IsPhotoEnclosed", Job.IsPhotoEnclosed)
                    
                };
            Parameters = parameters;
        }

        public Job Job { get; set; }

        public SqlParameter[] Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }
    }
    #endregion
}
